import axios from "axios"

const state = {
    adminName: localStorage.adminName,
    token: localStorage.token,
    adminLog: [],     //管理员登录日志,
    adminLIst: []
}
const mutations = {
    //登录与token
    CHANGE_ADMIN_INFO(state, data) {
        // console.log(state, adminName);
        state.adminName = localStorage.adminName = data.adminName;
        state.token = localStorage.token = data.token;
    },
    //退出登录
    OUT_LOGIN(state) {
        localStorage.clear();   //清除localStorage
        state.adminName = state.token = null;
    },
    //管理员登录日志数据修改
    CHANGE_ADMIN_LOG(state, Logdata) {
        state.adminLog = Logdata;
    },
    //增加管理员列表
    CHANGE_ADMIN_LIST(state, adminLIst) {
        state.adminLIst = adminLIst
    }
}
const actions = {
    //管理员登录
    login({ commit }, that) {
        axios.post("/login", that.ruleForm).then(data => {
            // console.log(data);
            if (data.ok === 1) {
                //   console.log( "登录成功");
                commit("CHANGE_ADMIN_INFO", data);
            } else {
                that.$message.error("账号或密码错误");
                that.ruleForm.adminName = null;
                that.ruleForm.passWord = null;
            }
        });
    },
    //请求管理员日志分页数据
    getAdminLog({ commit }, params) {
        // console.log('getadminList调用成功');
        axios.get('/adminLog', { params })
            .then(res => {
                // console.log(data);
                commit('CHANGE_ADMIN_LOG', res.Logdata);
                commit('CHANGE_PAGE_INDEX', res.pageIndex);
            });
    },
    //删除管理员日志记录
    deleteAdminLog(state, _id) {
        return axios.delete('/deleteAdminLog', {
            params: {
                _id
            }
        })
    },
    getAdminList({ commit }) {
        console.log(11111111);
        axios.get('/getAdminList').then(res => {
            commit('CHANGE_ADMIN_LIST', res.result);
            console.log(res);
        })
    }
}

export default {
    state, mutations, actions
}